package com.example.l2048;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.view.Gravity;
import android.widget.FrameLayout;

import androidx.core.content.ContextCompat;

public class Card extends androidx.appcompat.widget.AppCompatTextView {
    //public:
    //构造函数
    public Card(Context context,int _width, int _x, int _y){
        super(context);

        //设置卡片的大小
        cardParams = new FrameLayout.LayoutParams(_width,_width);
        this.setLayoutParams(cardParams);

        //设置圆角大小
        cardDrawable = new GradientDrawable();
        cardDrawable.setColor(Color.WHITE);             //设置默认颜色
        cardDrawable.setCornerRadius(20);               //设置圆角
        this.setBackground(cardDrawable);               //应用设置

        //设置位置
        currentPosition = new Point(_x, _y);
        datedPosition = new Point(_x, _y);
        cardParams.setMargins(_x,_y,0,0);

        //设置数值与控件样式
        this.SetCardNumber(0);
    }

    //获取卡片的数值
    public int GetCardNumber() {
        return this.cardNumber;
    }

    //设置数值与控件样式
    public void SetCardNumber(int _n){
        this.cardNumber = _n;
        //根据数值大小设置背景颜色
        switch (_n) {
            case 0:
                cardDrawable.setColor(Color.parseColor("#D5CCC3"));
                break;
            case 2:
                cardDrawable.setColor(Color.parseColor("#EDE3D5"));
                break;
            case 4:
                cardDrawable.setColor(Color.parseColor("#EBDFC7"));
                break;
            case 8:
                cardDrawable.setColor(Color.parseColor("#F2BE79"));
                break;
            case 16:
                cardDrawable.setColor(Color.parseColor("#F59562"));
                break;
            case 32:
                cardDrawable.setColor(Color.parseColor("#F47B51"));
                break;
            case 64:
                cardDrawable.setColor(Color.parseColor("#F55C3A"));
                break;
            case 128:
                cardDrawable.setColor(Color.parseColor("#ECCD70"));
                break;
            case 256:
                cardDrawable.setColor(Color.parseColor("#ECCB60"));
                break;
            case 512:
                cardDrawable.setColor(Color.parseColor("#E7CC77"));
                break;
            case 1024:
                cardDrawable.setColor(Color.parseColor("#ECC431"));
                break;
            case 2048:
                cardDrawable.setColor(Color.parseColor("#7FFF00"));
                break;
            default:
                cardDrawable.setColor(Color.parseColor("#AFEEEE"));
                break;
        }

        //设置文字样式
        this.setGravity(Gravity.CENTER);            //居中
        this.setTextSize(35);                       //设置文字大小
        this.getPaint().setFakeBoldText(true);      //加粗
        if(_n < 8) {                                //设置文字颜色
            this.setTextColor(Color.parseColor("#776E64"));
        }
        else {
            this.setTextColor(Color.parseColor("#F9F6EF"));
        }
        //设置数值
        if( _n == 0){
            this.setText("");
        }
        else{
            this.setText(String.valueOf(_n));
        }
    }

    //private:
    private int cardNumber = 0;                     //卡片上的数值
    private Point currentPosition;                  //当前位置
    private Point datedPosition;                    //之前位置
    private FrameLayout.LayoutParams cardParams;    //卡片控件样式
    private GradientDrawable cardDrawable;          //shape标签的代码实现
}
